package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000126624277";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQClDst/e4Os5e5LBO+qEQfgFBaxYVIpvSwVhvBp5zDMSCLXvKMlCt0Djz4Cu1u/E5OnXMEUSzvDasN9hvLHgcBCytW/GaSrWGyc7xwoBe5eB0O/CsaXa3xYklwtoraryLAoQGbRCU0ZcU9l7chXVyc4QpgtVYtGSUpL5FIw47FaCi7LN9Pwcu9QYiCtC+fT49npCA2Kd4/ckkerkrbGpuPUZUA0vNL/GKNuF9vhatpIJyHw3/wB7f8VxiE7AwGFhlHx/4SwXBzlSJmQuo4coeODhn9a9Hhd9ufv1eP2uJ0h0Kvml37acM7Wah9ztthSR4I/nHLYDvFFtQ8kRvbpXFxFAgMBAAECggEADgQEma2mT78yjeI8a6eIkKmEB8nozSyIGsfbkFKWXTJe6wzUmaejftLiBB9eugnyhsqkhpGg4IYnVXhpEzHcXo+UuOZ3iik3B9hx/qAKTqe1Q/qpRGBXLbo345QmiM3Az5owYN22U0vA3zniwi/fX7Kf83GJIw3EJk+b/xTGkYua5LzwgOkLlg/hLiYrcY0ja5apmquZy8bKVoaFdNknZVB26SfYPFDWlMifrnePg/Pp2C99IpKIiTFieGyTEqd1Ipx0H1SCrEgcIpXNTx21JW87grbYcH4nI7SBfxJDeJZQEP5SUeTO/XVxGVY2DtuLSDqbHU/VZ9YQqIDPUCgPQQKBgQDRx1g9u0erC4s8QJ2qsv6FbKb6MtVdMeggeOQdwsPLdAvPS7zjo0q7CkAUq9SmhsM7z1SF+og+yGpwhK518MA+0gRnX5xzuHlXEdBzaBPE+Eyo3Az2wShH1FWiTKIcl9qB1VMHvGPSaJ4gzeOR3ygUpET/8V88GkkrQ+3Qvc+OUQKBgQDJbPL9B1NzDDxccOBvGWqwxyyf6JNo7nG/6M9Cg8o+Nty4RsM9maxgkGjBIgZa+KonLEs6UNTi9EGfbKIgnc/KcF1uv6FA5gXYPuIDS01h+dfDurx80BJnKep3I1BNKWnruI7hvVlauGRTtxuSZT+fe7KQBidq2IzxVCjDf3CttQKBgQDGN4ssgVp9x2Gf5BpWod0Xa4v3W7L55V9F15UMfe0NwDAsho8LtU62nz/fiGFdnHa7e/OMySaV1D+m4+gJLxwn/8ADgXdObN/SotWJRq1vb4LYB10Eh40tGfh0a96+D8p65XwYEi1sMkhADOtb+TVhE7rS8FqhauD6TaxXkhKdkQKBgQCLXPZCW29BipW43+Bo/gzzgts9SI21bEUHouzD+KtVoF/WoB81372EkmXoOsJq7uHLeLoT/xlMbfN5epAyQP4j8tqyZaQMYxpoJBn8bX187IB8zBbdxJj3B+lRxP0qZh0DGw7A4WGRg0LnKSnKgIMktVE2lQtwwaPC1Zfeq64qgQKBgBlX80EH837Ag+liAbM8ctH81i/K4w9sgcsuQGjcmcTNGSt4xc54h7cByNxs5uoyBuJaYZUy0KGZtqe4uvDJ/IhuN5stFbPr3LYs5Y+mAMYA3HSjmDQPZFYf7rH19/bTcZDyGpoU1IputEr5PjLCO8DKbrharR34fJf5SkPzAab+";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm1QVR1OuAMtvhmCoXlMvuZJUmBvuhJ85FRUDwQtHxLjkHJG/M2bpYMWsMB2hbprEZy+VPnlP9CvodIvlpqrdQ2oN/SEkdBYe9ZZd2OQhE8Gf4xUcq40GoVT2dv5S/IWBxz5hFjgCKUO4AVJ9KydkJak+EklDLynUWTizWg/ZsNPJWXIi0FnAVDTWdfrwavm9FW0bb96Uml/UQKdiED1A1xBTUByfpUMutAKsZYyTgWsJvzP98NfS3vWYs8G7Q74rWttlVZ0hUl0zQuTG9qxE0/GXuDqTKSLyGbqi6FvWiq2jKz1HKC4+ZJjBea+zqBzE++gtBlLBS8lDe7fJvAVDQQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://8057eb3bi081.vicp.fun/paid/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    //   支付宝支付成功后,要跳转的页面地址
    private  String return_url ="http://member.gulimall.com/memberOrder.html";
//    private  String return_url ="http://8057eb3bi081.vicp.fun/alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","

                + "\"timeout_express\":\"1m\","

                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
